package com.cloud.gatewaySpringSecurity.handler;

import com.cloud.gatewaySpringSecurity.constant.TokenConstant;
import com.cloud.gatewaySpringSecurity.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.logout.ServerLogoutHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author zhozixiong
 * @date 2022/09/11
 *
 * 登出处理器
 */
@Component
public class LogoutHandler implements ServerLogoutHandler {

    @Autowired
    private RedisService redisService;

    @Override
    public Mono<Void> logout(WebFilterExchange webFilterExchange, Authentication authentication) {
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) authentication;
        redisService.del(TokenConstant.TOKEN_REDIS_PREFIX + authenticationToken.getCredentials());
        return Mono.empty();
    }
}
